import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-07-06
 * Time: 18:59
 */
public class MyLinkedList {
    public Node head;
    public Node last;
    static class Node{
        public int val;
        public Node prev;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        //第一次插入
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        //第一次插入
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws AddIndexOutExction {
        Node node = new Node(data);
        Node cur = head;
        //判断index的有效性
        if (index < 0 || index > size()) {
            throw new AddIndexOutExction("下标无效!");
        }
        //头插
        if (index == 0) {
            addFirst(data);
            return;
        }
        //尾插
        if (index == size()) {
            addLast(data);
            return;
        }
        //中间插
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev = node;
        node.prev.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = head;
        while(cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        Node cur = head;
        while(cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    //头节点
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    //中间 尾巴节点
                    if (cur.next != null) {
                        //中间
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {
                        //尾巴
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node cur = head;
        while(cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    //头节点
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    //中间 尾巴节点
                    if (cur.next != null) {
                        //中间
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {
                        //尾巴
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        Node cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        Node cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        Node cur = head;
        while(cur != null) {
            Node curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }

}
